import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:power_image/power_image.dart';
import 'package:video_player/video_player.dart';

void main() {
  CustomFlutterBinding();
  PowerImageLoader.instance.setup(PowerImageSetupOptions(renderingTypeTexture,
      errorCallback: (PowerImageLoadException exception) {
        final Map<dynamic, dynamic> args = <dynamic, dynamic>{};
        (exception?.nativeResult ?? <dynamic, dynamic>{}).forEach((key, value) {
          args[key] = value?.toString() ?? '';
        });
      }));
  runApp(MyApp());
}

class CustomFlutterBinding extends WidgetsFlutterBinding
    with BoostFlutterBinding {}

class MyApp extends StatelessWidget {
  Route<dynamic> routeFactory(RouteSettings settings, String uniqueId) {
    if (settings.name == 'A') {
      return PageRouteBuilder<dynamic>(settings: settings, pageBuilder: (_, __, ___) {
        return PageA();
      });
    } else {
      return PageRouteBuilder<dynamic>(settings: settings, pageBuilder: (_, __, ___) {
        return PageB();
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return FlutterBoostApp(routeFactory,);
  }
}

class PageA extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    print('TEST--->build--->1');
    return Scaffold(
      body: Center(
        child: _Video(),
      ),
    );
  }
}

class _Video extends StatefulWidget{
  @override
  _VideoState createState()=>_VideoState();
}

class _VideoState extends State<_Video>{



  @override
  Widget build(BuildContext context) {
    print('123 TEST--->build--->2');
    return Center(
      child: GestureDetector(
        onTap: () {
          BoostNavigator.instance.push('B',
            withContainer: true,
          );
        },
        child: _ButterFlyAssetVideo(),
      ),
    );
  }
}


class _ButterFlyAssetVideo extends StatefulWidget {
  @override
  _ButterFlyAssetVideoState createState() => _ButterFlyAssetVideoState();
}

class _ButterFlyAssetVideoState extends State<_ButterFlyAssetVideo>  with WidgetsBindingObserver {

  VideoPlayerController _controller = VideoPlayerController.asset('assets/xianyuhuapin.mp4');

  @override
  void initState() {
    super.initState();
    print('111 TEST--->initState--->');

    WidgetsBinding.instance.addObserver(this);

    _controller.addListener(() {
      setState(() {});
    });
    _controller.initialize();
    _controller.setLooping(true);
    _controller.play();

  }

  @override
  void dispose() {
    print('111 TEST--->dispose--->');

    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    // return Scaffold(
    //   body: Center(
    //     child: PowerImage.asset('images/IF7.jpg',
    //       renderingType: renderingTypeTexture,
    //       width: 300,
    //       height: 300,
    //     ),
    //   ),
    // );

    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Container(
            padding: const EdgeInsets.only(top: 20.0),
          ),
          const Text('With assets mp4'),
          Container(
            padding: const EdgeInsets.all(20),
            child: AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: Stack(
                alignment: Alignment.bottomCenter,
                children: <Widget>[
                  VideoPlayer(_controller),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}


class PageB extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: Center(

        child:TextButton(
            onPressed: () => {
              BoostNavigator.instance.pop()
            },
            child: Text('跳转')),
      ),
    );
  }
}